package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

/**
 * 处理相册相关的请求
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "01.相册管理模块")
@Slf4j
@Validated
@RequestMapping("/albums")
@RestController

public class AlbumController {

    @Autowired
    private IAlbumService albumService;
    public AlbumController(){
        log.debug("创建控制器对象: AlbumController");
    }
    //http://localhost:9080/albums/add-new?name=相册101&description=相册101的简介&sort=152
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 1)
    @PostMapping("/add-new")
    //                       ↓↓↓↓↓↓以下是新添加的注解
    public JsonResult<Void>  addNew(@Valid AlbumAddNewDTO albumAddNewDTO){
        log.debug("开始处理[添加相册]的请求,参数:{}",albumAddNewDTO);
            albumService.addNew(albumAddNewDTO);
            log.debug("测试添加数据成功!");
            return JsonResult.ok();

    }

    //http://localhost:9080/albums/9527/delete
    @ApiOperation("根据id删除相册")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParam(name = "id",value = "相册id",required = true,dataType = "long")
    @PostMapping( "/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1,message = "删除相册失败，尝试删除的相册的ID无效！")
                             @PathVariable Long id){
        log.debug("开始处理[根据id删除相册]的请求,参数: {}", id);
        albumService.delete(id);
        return JsonResult.ok();
    }

    //http://localhost:9080/albums/9527/update
    @ApiOperation("根据id修改相册详情")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParam(name = "id", value = "相册id", required = true, dataType = "long")
    @PostMapping( "/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, AlbumUpdateDTO albumUpdateDTO){
        log.debug("开始处理【修改相册详情】的请求，参数ID：{}, 新数据：{}", id, albumUpdateDTO);
        albumService.updateById(id,albumUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums/9527/select
    @ApiOperation("根据id查询相册详情")
    @ApiOperationSupport(order = 419)
    @GetMapping("/{id:[0-9]+}/select")
    public JsonResult<AlbumStandardVO> getStandardById(@Range(min = 1,message = "根据id查询相册详情失败，尝试查询的相册的ID无效！")
                                                           @PathVariable Long id) {
        log.debug("开始处理【根据id查询相册详情】的请求，无参数");
        AlbumStandardVO queryResult = albumService.getStandardById(id);
        //List<AlbumListItemVO> list = albumService.list();
        return JsonResult.ok(queryResult);
    }

    // http://localhost:8080/albums
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<AlbumListItemVO>> list() {
        log.debug("开始处理【查询相册列表】的请求，无参数");
        List<AlbumListItemVO> list = albumService.list();
        return JsonResult.ok(list);
    }

    //http://localhost:9080/albums/9527/delete
    @ApiOperation("[已过期]根据名称删除相册")
    @ApiOperationSupport(order = 3)
    @RequestMapping(value = "/{name:[a-z]+}/delete",method = RequestMethod.POST)
    public String deletePos(@PathVariable String name){
        String message = "尝试删除name值为[" + name + "]的相册";
        log.debug(message);
        return message;
    }
    //http://localhost:9080/albums/test/delete
    @ApiOperation("[已过期]测试删除相册")
    @ApiOperationSupport(order = 4)
    @RequestMapping(value = "/test/delete",method = RequestMethod.POST)
    public String deleteTest(){
        String message = "尝试测试删除相册";
        log.debug(message);
        return message;
    }


}
